
#include "MainMenu.h"

MainMenu::MainMenu(void)
{
}

MainMenu::~MainMenu(void)
{
}

void MainMenu::show() const
{	
    using std::cout;
    using std::endl;
    using std::ends;

    system("cls");
    cout << "Matrix Life 0.1 Alpha, Jul. 24, 2008\n\n"
         << "1. Run Demo\n"
         << "2. World Size Config\n"
         << "3. Loop Config\n"
         << "4. Life Config\n"
         << "5. Use Configured World\n"
         << "6. Use Randamly Generated World\n"
         << "7. Resume Last World\n"
         << "8. About\n"
         << "Q. Quit\n"
         << "------------------------\n"
         << "Your choice (1/2/3/4/5/6/7/Q): "
         << ends;
}

//TODO: refractor menus using subscriber pattern when dealing with
//      keyboard message.
void MainMenu::keyboardListener(char const& key)
{
    setFocus(false);
    switch (key)
    {
    case '1':
        MessageArray::Instance()->postMessage(SHOW_DEMOMENU);
        break;
    case '2':
        MessageArray::Instance()->postMessage(CONFIG_WORLD_SIZE);
        break;
    case '3':
        MessageArray::Instance()->postMessage(CONFIG_LOOP_TIME);
        break;
    case '4':
        MessageArray::Instance()->postMessage(CONFIG_LIFE);
        break;
    case '5':
        MessageArray::Instance()->postMessage(RUN_CONFIGWORLD);
        break;
    case '6':
        MessageArray::Instance()->postMessage(RUN_RANDOMWORLD);
        break;
    case '7':
        MessageArray::Instance()->postMessage(RUN_RESUMELAST);
        break;
    case '8':
        MessageArray::Instance()->postMessage(SHOW_ABOUTDLG);
        break;
    case 'Q':
    case 'q':
        MessageArray::Instance()->postMessage(QUIT);
        break;
    default:
        std::cout << "Command not found." << std::endl;
        break;
    }
}

bool MainMenu::fire()
{
    U_MESSAGE msg(MessageArray::Instance()->getMessage());
    switch (msg)
    {
    case SHOW_MAINMENU:
        setFocus(true);
        show();
        return true;
        break;
    default:
        if (getFocus())
        {
            keyboardListener(static_cast<char>(msg));
            return true;
        }
        break;
    }
    return false;
}
